﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanmia.Core;
using Tanmia.Basis.Domain;

namespace Tanmia.Domain
{
    public class VoucherDomain : BaseDomainObject<Voucher>
    {
        public IList<Voucher> GetVouchers(Account account)
        {
            return repository.Gets(x => x.Details.Any(y => y.Account.AccountID == account.AccountID));
        }

        public IList<Voucher> GetVouchers(Account account, DateTime from, DateTime to)
        {
            return repository.Gets(x =>
                x.Details.Any(y => y.Account.AccountID == account.AccountID) &&
                (x.CreateDate.Date >= from.Date && x.CreateDate.Date <= to.Date)
                );
        }


        public IList<VoucherDetail> GetStatement(Account account)
        {
            return GetSatementCore(GetVouchers(account), account);
        }

        public IList<VoucherDetail> GetStatement(Account account, DateTime from, DateTime to)
        {
            return GetSatementCore(GetVouchers(account, from, to), account);
        }

        private IList<VoucherDetail> GetSatementCore(IList<Voucher> vocherList, Account account)
        {
            return vocherList
                .SelectMany(x => x.Details)
                .Where(y => y.Account.AccountID == account.AccountID)
                .ToList<VoucherDetail>();

            //IList<VoucherDetail> reVal = new List<VoucherDetail>();

            //foreach (var item in vocherList)
            //{
            //    foreach (var detail in item.Details)
            //    {
            //        if (detail.Account.AccountID == account.AccountID)
            //            reVal.Add(detail);
            //    }
            //}

            //return reVal;
        }


        public double GetTotalAccount(Account account, PersonTypeEnum personType)
        {
            return GetTotalAccount(GetStatement(account), personType);
        }

        public double GetTotalAccount(Account account, DateTime from, DateTime to, PersonTypeEnum personType)
        {
            return GetTotalAccount(GetStatement(account, from, to), personType);
        }

        public double GetTotalAccount(IList<VoucherDetail> statement, PersonTypeEnum personType)
        {
            return AccountingCore(statement, personType);
        }

        private double AccountingCore(IList<VoucherDetail> statement, PersonTypeEnum personType)
        {
            double totalCredit = 0, totalDebit = 0;

            if (statement.Count == 0)
                return 0;
            else
                foreach (VoucherDetail item in statement)
                {
                    totalCredit += item.Credit;
                    totalDebit += item.Debit;
                }

            if (personType == PersonTypeEnum.Customer)
                return totalDebit - totalCredit;
            else
                return totalCredit - totalDebit;

        }

    }
}
